﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using BE;
using BE.SearchCriteria;


namespace DAL
{
    public class ZonaDAL : BaseDAL
    {
        public List<ZonasBE> SeleccionarPorParametros(BaseSC pFiltroSC, int pPageSize, int? pPageNum, ref int pCountReg)
        {
            ZonaSC wFiltroSC = (ZonaSC)pFiltroSC;
            if (wFiltroSC == null)
                wFiltroSC = new ZonaSC();

            List<ZonasBE> listaDeZonaBE = null;
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@IdZona", wFiltroSC.IdZona));
                cmd.Parameters.Add(new SqlParameter("@IdDivision", wFiltroSC.IdDivision));
                cmd.Parameters.Add(new SqlParameter("@Descripcion", wFiltroSC.Descripcion));
                cmd.Parameters.Add(new SqlParameter("@pagesize", pPageSize));
                cmd.Parameters.Add(new SqlParameter("@pagenum", pPageNum));

                SqlParameter p = new SqlParameter();
                p.ParameterName = "@PageCount";
                p.Direction = ParameterDirection.Output;
                p.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(p);

                cmd.CommandText = "Zonas_GetAllPaginated";
                cnn.Open();
                SqlDataReader reader = null;
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    Int32 idZonaIndex = reader.GetOrdinal("IdZona");
                    Int32 descripcionIndex = reader.GetOrdinal("Descripcion");

                    listaDeZonaBE = new List<ZonasBE>();

                    while (reader.Read())
                    {
                        ZonasBE oZonaBE = new ZonasBE();
                        Object[] values = new Object[reader.FieldCount];
                        reader.GetValues(values);

                        oZonaBE.IdZona = Convert.ToInt32(values[idZonaIndex]);
                        oZonaBE.Descripcion = Convert.ToString(values[descripcionIndex]);
                        oZonaBE.IdDivision = Convert.ToInt32(reader["IdDivision"]);
                        if (reader["IdVendedorGteZonal"] != DBNull.Value)
                        {
                            oZonaBE.IdVendedorGteZonal = Convert.ToInt32(reader["IdVendedorGteZonal"]);
                            oZonaBE.GerenteZonal = Convert.ToString(reader["Vendedor"]);
                        }
                        listaDeZonaBE.Add(oZonaBE);
                    }
                }
                reader.Close();
                pCountReg = (Int32)cmd.Parameters["@PageCount"].Value;

            }
            return listaDeZonaBE;
        }

        public void Insertar(ZonasBE pElemBE)
        {
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Zonas_i";
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", pElemBE.Descripcion));
                    cmd.Parameters.Add(new SqlParameter("@IdDivision", pElemBE.IdDivision));                    
                    cmd.Parameters.Add(new SqlParameter("@IdVendedorGteZonal", pElemBE.IdVendedorGteZonal));
                    SqlParameter p = new SqlParameter();
                    p.ParameterName = "@IdZona";
                    p.Direction = ParameterDirection.Output;
                    p.SqlDbType = SqlDbType.Int;
                    cmd.Parameters.Add(p);

                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    pElemBE.IdZona = (Int32)cmd.Parameters["@IdZona"].Value;
                }
                catch (Exception)
                {
                    throw;
                }

            }
        }

        public void Modificar(ZonasBE pElemBE)
        {
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Zonas_u";
                    cmd.Parameters.Add(new SqlParameter("@IdZona", pElemBE.IdZona));
                    cmd.Parameters.Add(new SqlParameter("@IdDivision", pElemBE.IdDivision));
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", pElemBE.Descripcion));
                    cmd.Parameters.Add(new SqlParameter("@IdVendedorGteZonal", pElemBE.IdVendedorGteZonal));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    throw;
                }

            }
        }

        public void Delete(int pIdZona)
        {
            try
            {
                using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Zonas_d";
                    cmd.Parameters.Add(new SqlParameter("@IdZona", pIdZona));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            { throw; }

        }


    }
}
